<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
class OrderController extends Controller
{
    //订单列表
    public function index(Request $request)
    {
    	$num = $request -> input('num',10);
    	$keyword = $request -> input('keyword','');
        $order = $request -> input('order','');
        dd($order);
    	//查询订单数据
    	$data = DB::table('order') -> where('status',0) -> where('goods','like','%'.$keyword.'%') -> orderBy('time','desc') -> paginate($num);

    	foreach($data as $k => $v)
    	{
    		$v -> time = date('Y-m-d',$v -> time);
    		
    	}
    	
    	//查询提交订单的用户名
    	$userName = DB::table('users') -> join('order','users.id','=','order.uid') -> select('users.name') -> get();

    
    	return view('admin.orders.index',['data' => $data,'request' => $request -> all(),'userName' => $userName]);
    }

    //删除订单
    public function delete($id)
    {
    	$res = DB::table('order') -> where('id',$id) -> update(['status' => 1]);

    	if($res)
        {
            return back() -> with(['info' => '删除成功']);
        }else
        {
            return back() -> with(['info' => '删除失败']);
        }
    	
    }

    //回收站管理
    public function recycle(Request $request)
    {
    	$num = $request -> input('num',10);
    	$keyword = $request -> input('keyword','');

    	//查询订单数据
    	$data = DB::table('order') -> where('status',1) -> where('goods','like','%'.$keyword.'%') -> paginate($num);

    	foreach($data as $k => $v)
    	{
    		$v -> time = date('Y-m-d',$v -> time);
    		
    	}
    	
    	//查询提交订单的用户名
    	$userName = DB::table('users') -> join('order','users.id','=','order.uid') -> select('users.name') -> get();

    
    	return view('admin.orders.recycle',['data' => $data,'request' => $request -> all(),'userName' => $userName]);
    }

    //彻底删除
    public function sureDel($id)
    {
    	$res = DB::table('order') -> delete($id);
    	if($res)
    	{
    		return back() -> with(['info' => '删除成功']);
    	}else
    	{
    		return back() -> with(['info' => '删除失败']);
    	}
    }

    //恢复订单
    public function unsureDel($id)
    {
    	$res = DB::table('order') -> where('id',$id) -> update(['status' => 0]);

    	if($res)
    	{
    		return back() -> with(['info' => '恢复成功']);
    	}else
    	{
    		return back() -> with(['info' => '恢复失败']);
    	}
    }
}
